package com.zjs.jsp.mongodbapi.service.mongodb;

import com.mongodb.WriteResult;
import com.zjs.jsp.domain.jsp.Jsp_Task;
import com.zjs.jsp.mongodbapi.service.TkService;
import com.zjs.jsp.mongodbapi.service.domain.OrderData;
import com.zjs.jsp.mongodbapi.service.domain.RootData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 拓展mong
 *
 * @author 李文
 * @create 2017-09-01 17:25
 **/
@Component
public class JspTaskExpand
{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TkService tkService;


    private static final Logger LOGGER=LoggerFactory.getLogger(JspTaskExpand.class);

    /**
     * 通过 单号 更新 下发状态为0
     *
     * @param pocs 任务编码
     */
    public Boolean updateXfzt(List<String> pocs) {
        WriteResult m = mongoTemplate.upsert(new Query(Criteria.where("od.poc").in(pocs)), new Update().set("xfzt", 0), Jsp_Task.class);
        return m.wasAcknowledged();
    }

    /**
     * 通过 tk 更新   任务类型
     *
     * @param tk   任务编码
     * @param rwlx 10 自动分配  20 抢单  30  强制分配
     */
    public Boolean updateRwlx(String tk, Integer rwlx) {
        WriteResult m = mongoTemplate.upsert(new Query(Criteria.where("tk").is(tk)), new Update().set("rwlx", rwlx), Jsp_Task.class);
        return m.wasAcknowledged();
    }

    /**
     * 通过 tk 更新   派送状态
     *
     * @param tk   任务编码
     * @param czzt 40  已派送
     */
    public Boolean updateCzzt(List<String> tk, Integer czzt) {

        List<Jsp_Task> list = mongoTemplate.find(new Query(Criteria.where("tk").in(tk)), Jsp_Task.class);
        if (list != null && list.size() > 0) {
            WriteResult m = mongoTemplate.updateMulti(new Query(Criteria.where("tk").in(tk)),
                    new Update().set("czzt", czzt), Jsp_Task.class);
            return m.wasAcknowledged();
        } else {

            List<String> pocList = new ArrayList<>();
            for (String s : tk) {
                RootData data = tkService.getHisOrdersByTaskCode(s);
                LOGGER.info(data.toString());
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    for (OrderData o : data.getData()) {
                        pocList.add(o.getOrderCode());
                    }
                }
            }
            if (pocList.size() > 0) {
                return updatePocToCzzt(pocList, czzt);
            }else{
                return updatePocToCzzt(tk, czzt);
            }
        }
    }

    public Boolean updateCzzt(List<String> orderCode, String czzt) {

        WriteResult m = mongoTemplate.updateMulti(new Query(Criteria.where("od.poc").in(orderCode)),
                new Update().set("od.$.ddzt", czzt), Jsp_Task.class);
        return m.wasAcknowledged();
    }

    private Boolean updatePocToCzzt(List<String> orderCode, Integer czzt) {

        WriteResult m = mongoTemplate.updateMulti(new Query(Criteria.where("od.poc").in(orderCode)),
                new Update().set("czzt", czzt), Jsp_Task.class);
        return m.wasAcknowledged();
    }


}
